package com.zhz.system.strategy.handler;

import com.zhz.common.operator.OperatorRouter;
import com.zhz.common.utils.ConvertUtils;
import com.zhz.model.system.contract.dto.PermissionMenuDTO;
import com.zhz.model.system.contract.param.PermissionMenuQueryParam;
import com.zhz.system.contract.enums.PermissionTypeEnum;
import com.zhz.system.contract.request.PermissionSaveRequest;
import com.zhz.system.contract.request.PermissionUpdateRequest;
import com.zhz.system.contract.response.PermissionFindByIdResponse;
import com.zhz.system.repository.service.PermissionMenuService;
import com.zhz.system.strategy.PermissionTypeOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 权限类型为菜单的操作
 *
 * @author zhouhengzhe
 * @date 2023/3/30
 */
@Component
public class PermissionMenuOperator implements PermissionTypeOperator {

    @Autowired
    private PermissionMenuService permissionMenuService;

    /**
     * Operator的名称, 同一类型的Operator的路由器{@link OperatorRouter}能够根据该值路由到当前的Operator
     *
     * @return route key
     */
    @Override
    public Integer getName() {
        return PermissionTypeEnum.PERMISSION_MENU.getCode();
    }

    /**
     * 添加对应的数据--添加权限菜单
     *
     * @param permissionSaveRequest 权限保存请求
     * @param pid                   权限id
     */
    @Override
    public void add(PermissionSaveRequest permissionSaveRequest, String pid) {
        PermissionMenuDTO permissionMenuDTO = ConvertUtils.sourceToTarget(permissionSaveRequest, PermissionMenuDTO.class);
        permissionMenuDTO.setPid(pid);
        permissionMenuService.save(permissionMenuDTO);
    }

    /**
     * 更新权限菜单信息
     *
     * @param permissionUpdateRequest 权限修改请求
     * @param pid                     权限id
     */
    @Override
    public void update(PermissionUpdateRequest permissionUpdateRequest, String pid) {
        PermissionMenuDTO permissionMenuDTO = permissionMenuService.getOne(PermissionMenuQueryParam.builder().pid(pid).build());
        permissionMenuDTO.setPid(pid)
                .setMenuIcon(permissionUpdateRequest.getMenuIcon())
                .setMenuOrder(permissionUpdateRequest.getMenuOrder());
        permissionMenuService.update(permissionMenuDTO);
    }

    /**
     * 根据id查询
     *
     * @param permissionFindByIdResponse 权限查询响应
     * @param id                         主键
     * @return 权限查询响应
     */
    @Override
    public PermissionFindByIdResponse findById(PermissionFindByIdResponse permissionFindByIdResponse, String id) {
        PermissionMenuDTO permissionMenuDTO = permissionMenuService.getOne(PermissionMenuQueryParam.builder().pid(id).build());
        if (Objects.nonNull(permissionMenuDTO)) {
            permissionFindByIdResponse.setMenuIcon(permissionMenuDTO.getMenuIcon())
                    .setMenuOrder(permissionMenuDTO.getMenuOrder());
        }
        return permissionFindByIdResponse;
    }

    /**
     * 根据业务key删除
     *
     * @param id 主键
     */
    @Override
    public void deleteById(String id) {
        permissionMenuService.deleteByKey(id);
    }
}
